package com.xcity.game.achievement.helper;

import com.xcity.game.achievement.Achievement;
import com.xcity.game.achievement.Achievement.State;
import com.xcity.game.achievement.AchievementHelper;
import com.xcity.game.achievement.AchievementTemplate;

/**
 * 成就类型：功能使用<br/>
 * 2个参数：①系统id②次数
 * @author yang.li
 *
 */
public class UseSystemHelper implements AchievementHelper {

	@Override
	public boolean update(Achievement refer, int... vals) {
		String sysId = String.valueOf(obtainSystemId(vals));
		int count = obtainUseCount(vals) + refer.getValue().getIntValue(sysId);
		refer.getValue().put(sysId, count);
		return isFinished(refer);
	}

	@Override
	public boolean isFinished(Achievement refer) {
		if (refer.getState() == State.INITED) {
			int sysId = obtainSystemId(refer.getTemplate());
			int count = obtainUseCount(refer.getTemplate());
			if (refer.getValue().getIntValue(String.valueOf(sysId)) >= count) {
				refer.setState(State.FINISHED);
				return true;
			}
		}
		return false;
	}
	
	static int obtainSystemId(int... vals) {
		return vals != null && vals.length > 0 ? vals[0] : 0;
	}
	
	static int obtainSystemId(AchievementTemplate template) {
		return (int) template.getCondition(0);
	}
	
	static int obtainUseCount(int... vals) {
		return vals != null && vals.length > 1 ? vals[1] : 0;
	}
	
	static int obtainUseCount(AchievementTemplate template) {
		return (int) template.getCondition(0);
	}
	
}
